package org.ala.tcp_feign_client.spring.feign.results.impl;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeoutException;

import org.ala.tcp_feign_client.spring.feign.results.ITcpFeignClientResultsMapper;
import org.ala.tcp_feign_common.exceptions.SerializableException;
import org.ala.tcp_feign_common.msg.biz_resp.BizRespByteMessage;


/**
 * 半异步回调
 * <p>	接口的返回值是Future类型
 * <p>	需要额外提供timeout机制（服务端没给返回值的情况，msgId会一直在map里）
 *
 * @author 骆毅(ala)
 * @date 2024年4月24日
 */
public class BizRespResultSAsyncCallback extends AbstractBizRespResultCallback {

	
	/**
	 * 返回值
	 */
	protected Object resp;
	/**
	 * 阻塞Future
	 */
	protected CompletableFuture<Object> future;
	
	
	public BizRespResultSAsyncCallback(long msgId, long timeout, ITcpFeignClientResultsMapper resultsMapper) {
		super(msgId, timeout, resultsMapper);
		
		future = new CompletableFuture<>();
	}


	@Override
	public void triggeCallback(BizRespByteMessage msg) throws SerializableException {
		//	反序列化
		Object resp = buildResp(msg);
		future.complete(resp);
	}
	@Override
	public void triggeTimeout() {
		future.completeExceptionally(new TimeoutException("消息超时"));
	}


	public Object getResp() {
		return resp;
	}
	public void setResp(Object resp) {
		this.resp = resp;
	}
	public CompletableFuture<Object> getFuture() {
		return future;
	}
	public void setFuture(CompletableFuture<Object> future) {
		this.future = future;
	}
	
}
